Page({
  data: {
    userInfo: {},
    userStats: {
      collectionCount: 0,
      footprintCount: 0,
      points: 0
    },
    isLoggedIn: false
  },

  onLoad: function() {
    this.checkLoginStatus();
  },

  onShow: function() {
    this.checkLoginStatus();
  },

  // 检查登录状态
  checkLoginStatus: function() {
    const userInfo = wx.getStorageSync('userInfo');
    const userStats = wx.getStorageSync('userStats') || {
      collectionCount: 0,
      footprintCount: 0,
      points: 0
    };
    
    if (userInfo && userInfo.nickName) {
      this.setData({
        userInfo: userInfo,
        userStats: userStats,
        isLoggedIn: true
      });
    } else {
      this.setData({
        userInfo: {},
        userStats: userStats,
        isLoggedIn: false
      });
    }
  },

  // 用户登录
  userLogin: function() {
    // 创建默认用户信息
    const defaultUserInfo = {
      nickName: '青秀山游客',
      avatarUrl: '/images/default-avatar.png'
    };
    
    this.setData({
      userInfo: defaultUserInfo,
      isLoggedIn: true
    });
    
    wx.setStorageSync('userInfo', defaultUserInfo);
    
    wx.showToast({
      title: '登录成功',
      icon: 'success'
    });
  },

  // 用户退出登录
  userLogout: function() {
    const that = this;
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: function(res) {
        if (res.confirm) {
          wx.removeStorageSync('userInfo');
          that.setData({
            userInfo: {},
            isLoggedIn: false
          });
          
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          });
        }
      }
    });
  },

  // 更换头像
  changeAvatar: function() {
    if (!this.data.isLoggedIn) {
      this.userLogin();
      return;
    }

    const that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        const tempFilePath = res.tempFilePaths[0];
        
        // 直接更新头像
        that.updateUserAvatar(tempFilePath);
      },
      fail: function(err) {
        console.log('选择图片失败', err);
        wx.showToast({
          title: '选择图片失败',
          icon: 'none'
        });
      }
    });
  },

  // 更新用户头像
  updateUserAvatar: function(avatarUrl) {
    let userInfo = this.data.userInfo;
    userInfo.avatarUrl = avatarUrl;
    
    this.setData({
      userInfo: userInfo
    });
    
    wx.setStorageSync('userInfo', userInfo);
    
    wx.showToast({
      title: '头像更新成功',
      icon: 'success'
    });
  },

  // 修改昵称
  changeNickname: function() {
    if (!this.data.isLoggedIn) {
      this.userLogin();
      return;
    }

    const that = this;
    const currentNickname = this.data.userInfo.nickName;
    
    wx.showModal({
      title: '修改昵称',
      content: '',
      editable: true,
      placeholderText: '请输入新昵称',
      confirmText: '确定',
      cancelText: '取消',
      success: function(res) {
        if (res.confirm) {
          const newNickname = res.content.trim();
          if (newNickname) {
            if (newNickname.length > 10) {
              wx.showToast({
                title: '昵称不能超过10个字',
                icon: 'none'
              });
              return;
            }
            that.updateUserNickname(newNickname);
          } else {
            wx.showToast({
              title: '昵称不能为空',
              icon: 'none'
            });
          }
        }
      }
    });
  },

  // 更新用户昵称
  updateUserNickname: function(nickName) {
    let userInfo = this.data.userInfo;
    userInfo.nickName = nickName;
    
    this.setData({
      userInfo: userInfo
    });
    
    wx.setStorageSync('userInfo', userInfo);
    
    wx.showToast({
      title: '昵称修改成功',
      icon: 'success'
    });
  },

  // 其他导航方法
  navigateToMyCollection: function() {
    if (!this.checkLogin()) return;
    wx.navigateTo({
      url: '/pages/myCollection/myCollection'
    });
  },

  navigateToMyFootprint: function() {
    if (!this.checkLogin()) return;
    wx.navigateTo({
      url: '/pages/myFootprint/myFootprint'
    });
  },

  navigateToMyOrders: function() {
    if (!this.checkLogin()) return;
    wx.navigateTo({
      url: '/pages/myOrders/myOrders'
    });
  },

  navigateToFeedback: function() {
    if (!this.checkLogin()) return;
    wx.navigateTo({
      url: '/pages/feedback/feedback'
    });
  },

  navigateToSettings: function() {
    wx.navigateTo({
      url: '/pages/settings/settings'
    });
  },

  // 检查登录状态
  checkLogin: function() {
    if (!this.data.isLoggedIn) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        showCancel: true,
        confirmText: '去登录',
        success: (res) => {
          if (res.confirm) {
            this.userLogin();
          }
        }
      });
      return false;
    }
    return true;
  },

  showAbout: function() {
    wx.showModal({
      title: '关于我们',
      content: '青秀山一日游小程序\n版本：1.0.0\n为您提供优质的旅游服务体验',
      showCancel: false
    });
  }
});